import 'dart:io';

import 'package:flutter/material.dart';
import 'package:gold/utils/xfg_color.dart';
import 'package:gold/utils/xfg_size.dart';

/// Descriptions: 文字控件组件
/// User: tangzhentao
/// Date: 12:38 下午 2021/11/25
///

class XfgText extends StatelessWidget {
  final String text;
  final Color? color;
  final Color? backgroundColor;
  final FontWeight? bold;
  final double? height;
  final double? fontSize;
  final int? maxLines;
  final FontStyle? fontStyle;
  final TextOverflow? overflow;
  final TextAlign? textAlign;
  final List<InlineSpan> children;
  final TextDecoration? decoration;

  const XfgText(this.text, {
    super.key,
    this.color = XfgColor.text1,
    this.backgroundColor,
    this.bold,
    this.fontSize,
    this.maxLines,
    this.overflow,
    this.textAlign,
    this.children = const [],
    this.decoration,
    this.height,
    this.fontStyle,
  });

  @override
  Widget build(BuildContext context) {
    return Material(
      color: Colors.transparent,
      child: children.isEmpty ? _buildText() : _buildTextRich(),
    );
  }

  ///纯文本组件
  Text _buildText() {
    return Text(
      text,
      maxLines: maxLines,
      overflow: overflow,
      textAlign: textAlign,
      style: TextStyle(
        backgroundColor: backgroundColor,
        color: color,
        decoration: decoration,
        decorationColor: color,
        height: height?? (Platform.isIOS ? 1:null),
        fontWeight: bold,
        fontStyle: fontStyle,
        fontSize: fontSize?? XfgSize().text12,
      ),
    );
  }

  ///TextRich组件
  Text _buildTextRich() {
    return Text.rich(
      TextSpan(
        text: text,
        children: children,
      ),
      maxLines: maxLines,
      overflow: overflow,
      textAlign: textAlign,
      style: TextStyle(
        color: color,
        height: height?? (Platform.isIOS ? 1:null),
        decoration: decoration,
        decorationColor: color,
        fontWeight: bold,
        fontSize: fontSize?? XfgSize().text12,
        fontStyle: fontStyle,
      ),
    );
  }
}
